package dao.impl;

import bean.dto.VehicleChangeData;
import bean.pojo.Room;
import bean.pojo.User;
import bean.pojo.Vehicle;
import commons.dbHelper.DBHelper;
import dao.VehicleDao;

import java.util.ArrayList;
import java.util.List;

/**
 * dao.impl
 * User: 坚持
 * Date: 2024/12/18 11:29
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
public class VehicleDaoImpl implements VehicleDao {

    @Override
    public Long findByTotal( String vehicleLicensePlate, String ownerName,String vehicleType) {
        StringBuffer sql = new StringBuffer("select ve.vehicle_id,ps.parking_space_id,ps.owner_vehicle_id,ve.vehicle_license_plate," +
                "ve.owner_name,ve.owner_phone,ve.vehicle_type,ps.start_time from  vehicle ve LEFT JOIN parking_space ps on ve.vehicle_id=ps.parking_vehicle_id");
        List<Object>  parameters=new ArrayList<>();
        if (vehicleLicensePlate != null) {
            sql.append(" and ve.vehicle_license_plate like concat ('%',?,'%') ");
            parameters.add(vehicleLicensePlate);
        }
        if (ownerName != null) {
            sql.append(" and ve.owner_name like concat ('%',?,'%') ");
            parameters.add(ownerName);
        }
        if (vehicleType != null) {
            sql.append(" and ve.vehicle_type like concat ('%',?,'%') ");
            parameters.add(ownerName);
        }
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        List<VehicleChangeData> query = dbHelper.getBeanList(VehicleChangeData.class,sql.toString(),parameters.toArray());
        if (query!=null) {
            return (long) query.size();
        }
        return 0L;
    }

    @Override
    public Integer updateVehicle(VehicleChangeData vehicleChangeData) {
        String sql = " update vehicle set vehicle_license_plate=?,owner_name=?,owner_phone=?,vehicle_type=?  where vehicle_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,vehicleChangeData.getVehicleLicensePlate(),vehicleChangeData.getOwnerName(),
                vehicleChangeData.getOwnerPhone(),vehicleChangeData.getVehicleType(),vehicleChangeData.getVehicleId());
    }

    @Override
    public Integer deleteById(Integer vehicleId) {
        String sql = " delete from vehicle where vehicle_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,vehicleId);
    }


    @Override
    public Integer add(Vehicle vehicle) {
        String sql = " insert vehicle (vehicle_license_plate,owner_name,owner_phone,vehicle_type) values (?,?,?,?)  ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,vehicle.getVehicleLicensePlate(),vehicle.getOwnerName(),
                vehicle.getOwnerPhone(),vehicle.getVehicleType());
    }

    @Override
    public List<VehicleChangeData> findBy(Integer page, Integer limit , String vehicleLicensePlate, String ownerName,String vehicleType) {
        StringBuffer sql = new StringBuffer("SELECT ve.vehicle_id,ps.parking_space_id,ps.owner_vehicle_id,ve.vehicle_license_plate,ve.owner_name," +
        "ve.owner_phone,ve.vehicle_type,ps.start_time FROM vehicle ve LEFT JOIN parking_space ps ON ps .parking_vehicle_id = ve.vehicle_id WHERE ve.vehicle_id!=0");
        List<Object>  parameters=new ArrayList<>();
        if (vehicleLicensePlate != null) {
            sql.append(" and ve.vehicle_license_plate like concat ('%',?,'%') ");
            parameters.add(vehicleLicensePlate);
        }
        if (ownerName != null) {
            sql.append(" and ve.owner_name like concat ('%',?,'%') ");
            parameters.add(ownerName);
        }
        if (vehicleType != null) {
            sql.append(" and ve.vehicle_type like concat ('%',?,'%') ");
            parameters.add(ownerName);
        }
        sql.append(" order by ve.vehicle_id asc ");
        sql.append(" limit " + (page - 1) * limit + "," + limit);
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        return dbHelper.getBeanList(VehicleChangeData.class,sql.toString(),parameters.toArray());
    }
}
